package com.test.gl;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.util.AttributeSet;
import android.util.Log;
/**
 * The purpose of this project is to imitate the blurred image background effect in
 * Android 3D Gallery, when opening a photo bucket to view the thumbnail grid.
 * I utilized several key structures of Android 3D Gallery source codes like Texture.
 * Also referenced/enriched some of the logics used in Android 3D Gallery.
 * @author g2southpark
 *
 */
public class RenderView extends GLSurfaceView implements GLSurfaceView.Renderer{
	private static final String TAG = "RenderView";
	GL11 mGL;
    public Quad quad;
	float mAlpha;
	
	public RenderView(Context context) {
		super(context);
		quad = new Quad();
	}

	public RenderView(Context context, AttributeSet attrs) {
		super(context, attrs);
		quad = new Quad();
	}

    public void setAlpha(float alpha) {
        GL11 gl = mGL;
        gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE);
        gl.glColor4f(alpha, alpha, alpha, alpha);
        mAlpha = alpha;
    }

    public float getAlpha() {
        return mAlpha;
    }

    public void setColor(float red, float green, float blue, float alpha) {
        GL11 gl = mGL;
        gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE);
        gl.glColor4f(red, green, blue, alpha);
        mAlpha = alpha;
    }

    public void resetColor() {
        GL11 gl = mGL;
        gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_REPLACE);
        gl.glColor4f(1, 1, 1, 1);
    }
    
    //  interfaces required for GLSurfaceView.Render
    
    /**
     * The Surface is created/init()
     */
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {      


        GL11 gl2 = (GL11) gl;
        if (mGL == null) {
            mGL = gl2;
        } else {
            // The GL Object has changed.
            Log.i(TAG, "GLObject has changed from " + mGL + " to " + gl);
            mGL = gl2;
        }
        
        if (false) {
        	setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
        } else {
        	setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
        }
  

        // Increase the priority of the render thread.
        // This is commented out to give other threads more CPU.
        //Process.setThreadPriority(Process.THREAD_PRIORITY_DISPLAY);

        // Disable unused state.
        gl.glEnable(GL11.GL_DITHER);
        gl.glDisable(GL11.GL_LIGHTING);

        // Set global state.
        // gl.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);

        // Enable textures.
        gl.glEnable(GL11.GL_TEXTURE_2D);
        gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_REPLACE);

        // Set up state for multitexture operations. Since multitexture is
        // currently used
        // only for layered crossfades the needed state can be factored out into
        // one-time
        // initialization. This section may need to be folded into drawMixed2D()
        // if multitexture
        // is used for other effects.

        // Enable Vertex Arrays
        gl.glEnableClientState(GL11.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
        gl.glClientActiveTexture(GL11.GL_TEXTURE1);
        gl.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
        gl.glClientActiveTexture(GL11.GL_TEXTURE0);

        // Enable depth test.
        gl.glEnable(GL11.GL_DEPTH_TEST);
        gl.glDepthFunc(GL11.GL_LEQUAL);

        // Set the blend function for premultiplied alpha.
        gl.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA);

        // Set the background color.
        gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        gl.glClear(GL11.GL_COLOR_BUFFER_BIT);
 
       //Load the texture for the cube once during Surface creation

       quad.loadGLTexture(gl, this.getContext(), R.drawable.test1);

    }
    public void switchTexture(int id){
    	quad.loadGLTexture(mGL, this.getContext(), id);
    }
    /**
     * Here we do our drawing
     */
    public void onDrawFrame(GL10 gl) {
       //Clear Screen And Depth Buffer
       gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);   
       gl.glLoadIdentity();               //Reset The Current Modelview Matrix
       
       //Drawing
       gl.glTranslatef(0.0f, 0.0f, -5.0f);      //Move 5 units into the screen
      // gl.glScalef(1.5f, 1.0f, 1.0f);          //Scale the Cube to 80 percent, otherwise it would be too large for the screen

       
       quad.draw(gl);                  //Draw the Quad   

       gl.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA);
       gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    }

    /**
     * If the surface changes, reset the view
     */
    public void onSurfaceChanged(GL10 gl, int width, int height) {
       if(height == 0) {                   //Prevent A Divide By Zero By
          height = 1;                   //Making Height Equal One
       }
       // Set the viewport and projection matrix.
       final float zNear = 0.1f;
       final float zFar = 100.0f;
       gl.glViewport(0, 0, width, height);
       gl.glMatrixMode(GL11.GL_PROJECTION);
       gl.glLoadIdentity();
       GLU.gluPerspective(gl, 45.0f, (float) width / height, zNear, zFar);

       gl.glMatrixMode(GL11.GL_MODELVIEW);
       gl.glLoadIdentity();

    }
}
